﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.Model;
using System.Data;
using Honestysoft.BaseLibrary.ExceptionHandling;

namespace Honestysoft.Financing.WebUI.Module.Attach
{
    public partial class ProjectFileEdit2 : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                BindProject();
                BindProjectDocument();
                //dtUser = new BaseBusinessLogic<Sys_User>(Globals.CurrentConnStringName).GetTableByKey(lsUserID.ToArray());
                //if (Request.QueryString["ForOpen"] == "1")//打开命令，必定传入项目ID和附件类型
                //{
                //    //得到附件，如果有则打开
                //    QueryParameters query = new QueryParameters();
                //    query.Add("DataID", Request.QueryString["ID"], QueryParameters.OperatorType.Equal);
                //    query.Add("AttachmentBuType", Request.QueryString["AttachmentBuType"], QueryParameters.OperatorType.Equal);
                //    List<Sys_Attachment> lsAtt = new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName).GetEntitys(query);
                //    //if (lsAtt != null && lsAtt.Count == 1)
                //    //{
                //    //    hidForOpen.Value = Globals.Home + lsAtt[0].Path + "/" + lsAtt[0].FileName + "." + lsAtt[0].Extension;
                //    //}
                //}
            }
        }

        //protected string _UploadPath
        //{
        //    get
        //    {
        //        object obj = ViewState["_UploadPath"];
        //        return obj == null ? "" : obj.ToString();
        //    }
        //    set
        //    {
        //        ViewState["_UploadPath"] = value;
        //    }
        //}

        protected override void ButtonSetting()
        {
            base.ButtonSetting();
            if (!PageSecurity.Delete)
            {
                btnDelete.Enabled = false;
            }
        }

        private void BindProject()
        {
            BD_Project project = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetBaseProject(Request.QueryString["ID"]);
            lblOwner.Text = string.IsNullOrEmpty(project.DOwnerName) ? project.OwnerName : ("建设单位：" + project.OwnerName + "<br/>委托代建单位：" + project.DOwnerName);// project.OwnerName;
            lblOwner971.Text = project.Owner971Name;
            lblProjectName.Text = project.Name;
            lblProjectSN.Text = project.SN;
            //_UploadPath = "Attachment/" + project.SN+project.Name + "/基础资料";
        }
        private void BindProjectDocument()
        {            
            QueryParameters query = new QueryParameters();
            query.Add("DataID", Request.QueryString["ID"], QueryParameters.OperatorType.Equal);
            query.Add("AttachmentBuType", ((int)Honestysoft.Financing.Model.EnumItem.AttachmentBuType.ProjectDocument).ToString(), QueryParameters.OperatorType.Equal);
            OrderParameters order = new OrderParameters();
            order.Add("CreateTime");
            int pageCount = 0, recordCount = 0;
            DataTable dt = new BaseBusinessLogic<Sys_Attachment>(Globals.CurrentConnStringName).GetTablePaging(query, order,AspNetPager1.PageSize,AspNetPager1.CurrentPageIndex,out pageCount,out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dt;
            rptList.DataBind();
        }

        protected string GetTextUrl(string fileName, string ext, string path)
        {
            string url = "";
            url += "<a href='" + Globals.Home + path + "/" + fileName + "." + ext + "' target='_blank'>";
            url += fileName;
            url += "</a>";
            return url;
        }

        protected string GetIcon(string ext)
        {
            if (string.IsNullOrEmpty(ext))
            {
                return "";
            }
            return "<img src='../../Uploadify/fileType/" + ext + ".gif' border='0' alt='' />";
        }

        protected void btnRebind_Click(object sender, EventArgs e)
        {
            BindProjectDocument();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindProjectDocument();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string[] arrId = GetSelectedRowKeys();
                BaseBusinessLogic<Sys_Attachment> baseLogic = new BaseBusinessLogic<Sys_Attachment>();
                baseLogic.DeleteByKey(arrId);
                ScriptHelper.Window.Alert("删除成功！");
                BindProjectDocument();
            }
            catch (Honestysoft.BaseLibrary.ExceptionHandling.BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("删除失败！系统发生了未知错误！");
            }
        }
    }
}